package main

// 713. 乘积小于 K 的子数组
func main() {
	println(numSubarrayProductLessThanK([]int{1, 1, 1}, 2))

}

func numSubarrayProductLessThanK(nums []int, k int) int {
	// sort.Slice(nums, func(i, j int) bool {
	// 	return nums[i] < nums[j]
	// })
	i, j := 0, 1
	count := 0
	for i < len(nums) {
		if nums[i] < k {
			count++
		}
		i++
	}
	i, j = 0, 1
	product := nums[0] * nums[1]
	for i < j && j < len(nums) {
		if product < k {
			count++
			if j == len(nums)-1 {
				product /= nums[i]
				i++
			} else {
				j++
				product *= nums[j]
			}
		} else if product >= k {
			product /= nums[i]
			i++
			if i == j && j < len(nums)-1 {
				j++
				product *= nums[j]
			}
		}
	}
	return count
}
